package com.songshuai.webapi.jobapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.songshuai.webapi.jobapi.common.enumerations.JobType;
import com.songshuai.webapi.jobapi.common.settings.CacheKeys;
import com.songshuai.webapi.jobapi.dao.productcenter.YixueCourseMapper;
import com.songshuai.webapi.jobapi.dto.base.QueryResponse;
import com.songshuai.webapi.jobapi.dto.productcenter.CourseBindDto;
import com.songshuai.webapi.jobapi.model.productcenter.YixueCourse;
import com.songshuai.webapi.jobapi.service.CourseBindService;
import com.songshuai.webapi.jobapi.service.JobNetPostService;
import com.songshuai.webapi.jobapi.service.SyncLogService;
import com.songshuai.webapi.jobapi.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Auther: Administrator
 * @Date: 2018/10/31 0031 19:46
 * @Description:
 */
@Component
@Slf4j
public class CourseBindServiceImpl implements CourseBindService {

    @Autowired
    YixueCourseMapper yixueCourseMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    JobNetPostService postService;

    @Autowired
    SyncLogService syncLogService;

    /**
     * 同步课程信息
     */
    @Override
    public void SyncNewYiXueCourse(Boolean isFullDay,String beginTime,String endTime) {
        HashOperations<String,String,String> operations= redisTemplate.opsForHash();
        String begin =operations.get(CacheKeys.JOB_TIME_CACHE_KEY, String.valueOf(JobType.CourseBind.getCode()));
        if(isFullDay||begin==null||begin=="") {
            begin = Util.ChangeDateToString(new Date(), "yyyy-MM-dd");
        }
        if(!Util.IsNullAndEmpty(beginTime)){
            begin=beginTime;
        }
        String currentTime=Util.ChangeDateToString(new Date(),"yyyy-MM-dd HH:mm:ss");
        log.info("课程信息同步开始");
        Integer limit = 20;
        Integer id = 0;
        Integer tempIndex=0;
        List<YixueCourse> courses=yixueCourseMapper.getYixueCoursesForJob(id,limit,begin,endTime);
        while (courses!=null&&courses.size()>0){
            tempIndex++;
            List<CourseBindDto>courseBindList=new ArrayList<CourseBindDto>();
            log.info("课程信息同步_"+tempIndex);
            for(YixueCourse yixueCourse:courses){
                id=yixueCourse.getId();
                CourseBindDto bindDto=new CourseBindDto();
                bindDto.setCourse_id(yixueCourse.getHt_id()==null?0L:yixueCourse.getHt_id());
                bindDto.setCrm_course_id(yixueCourse.getId()==null?0L:yixueCourse.getId().longValue());
                bindDto.setCrm_course_name(yixueCourse.getCname()==null?"":yixueCourse.getCname());
                bindDto.setGw_course_id(yixueCourse.getGw_id()==null?0L:yixueCourse.getGw_id());
                bindDto.setGw_course_name(yixueCourse.getGw_name()==null?"":yixueCourse.getGw_name());
                bindDto.setCourse_name("");
                courseBindList.add(bindDto);
            }
            if(courseBindList.size()>0){
                String listData= JSON.toJSONString(courseBindList);
                Map<String,String> parms=new HashMap<>();
                parms.put("list",listData);
                QueryResponse<Map<String,Boolean>> resMap= postService.PostFormData(parms,"course/courseBind");
                if(resMap!=null){
                    syncLogService.AddCourseLogs(JobType.CourseBind.getCode(),courseBindList,resMap.getData());
                }
                System.out.println(resMap);
            }
            if(courses.size()<limit){
                break;
            }
            courses=yixueCourseMapper.getYixueCoursesForJob(id,limit,begin,endTime);
        }
        if(!isFullDay){
            operations.put(CacheKeys.JOB_TIME_CACHE_KEY,String.valueOf(JobType.CourseBind.getCode()),currentTime);
        }
        log.info("课程信息同步结束");
    }
}
